/*
 * JaLingo, http://jalingo.sourceforge.net/
 *
 * Copyright (c) 2002-2006 Oleksandr Shyshko
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package uwncorrector.centre.gui.components.filechooser;

import uwncorrector.centre.util.io.Files;
import uwncorrector.client.application.util.Components;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class FileChooser {
    private JPanel gui;

    private JTextField field;
    private JButton button;
    private JFileChooser chooser;

    public FileChooser() {
        field = Components.textField();// TODO must be on LF level

        button = new JButton(" ... ");
        button.addActionListener(new ChooseFileListener());

        // TODO make button small as posible - rewrite this hack?? (with knowledge of LF)
        if (button.getBorder() instanceof CompoundBorder)
            button.setBorder(((CompoundBorder)button.getBorder()).getOutsideBorder());

        chooser = new JFileChooser(new File( "." ));
        chooser.setMultiSelectionEnabled(true);

        gui = new JPanel();
        gui.setLayout(new BorderLayout(5, 5));
        gui.add(field, BorderLayout.CENTER);
        gui.add(button, BorderLayout.EAST);
    }

    public JPanel getGui() {
        return gui;
    }

    public JTextField getField() {
        return field;
    }

    public JButton getButton() {
        return button;
    }

    public JFileChooser getChooser() {
        return chooser;
    }

    public void resetSelectedFiles() {
        field.setText("");
    }

    public boolean hasSelectedFiles() {
        boolean res = true;
        String[] paths = getSelectedPaths();
        for (int i = 0; i < paths.length && res; i++)
            res = Files.exists(paths[i]);
        return res;
    }

    public String[] getSelectedPaths() {
        String[] parts = field.getText().split(";");
        int cnt = parts.length;
        String[] paths = new String[cnt];
        for (int i = 0; i < cnt; i++)
            paths[i] = parts[i].trim();
        return paths;
    }

    private void setSelectedFile(File[] selectedFiles) {
        StringBuilder text = new StringBuilder();
        for (int i = 0; i < selectedFiles.length; i++) {
            if (i > 0)
                text.append("; ");
            text.append(selectedFiles[i].getPath());
        }
        field.setText(text.toString());
    }

    public void askForFile() {
        askForFile(button);
    }

    public void askForFile(Component parent) {
        String[] selPaths = getSelectedPaths();
        File[] files = new File[selPaths.length];
        for (int i = 0; i < files.length; i++)
            files[i] = new File(selPaths[i]);
        chooser.setSelectedFiles(files);
        
        int result = chooser.showOpenDialog(parent);
        if (result == JFileChooser.APPROVE_OPTION )
            setSelectedFile(chooser.getSelectedFiles());
    }


    private class ChooseFileListener implements ActionListener {
        public void actionPerformed( ActionEvent e ) {
            askForFile( button );
        }
    }
}
